package com.game.units {
	import com.game.data.Data;
	import com.game.utils.TowerType;
	import flash.display.Shape;
	public class Tower extends Shape {
		private var _posX:int = 0;
		private var _posY:int = 0;
		private var _upgrades:uint = 0;
		private var _step:int = Data.STEP;
		private var _type:TowerType = TowerType.NONE;
		private var _range:int = 0;
		private var _upgradeCost:int = 0;
		private var _cost:int;
		private var SHOOT_INTERVAL:Number = 10;
		/* amount of time until the base can shoot again - "cool down time" */
		private var _counterShoot:int = 0;
		public var shoot:Boolean = false;
		private var towerGraphics:TowerGraphics;
		public function Tower() {
			towerGraphics = new TowerGraphics(this.graphics);
		}
		public function init(posX:int, posY:int, type:TowerType, upgradeCost:int, range:int, cost:int):void {
			_type = type;
			typeConfig();
			_posX = posX;
			_posY = posY;
			_range = range;
			_upgradeCost = upgradeCost;
			_upgrades = 0;
			_cost = cost;
			x = _posX * _step + _step / 2;
			y = _posY * _step + _step / 2;
			towerGraphics.init(_type.ordinal);
			towerGraphics.redraw();
		}
		public function reset():void {
			//TODO: reset tower animation
			_counterShoot=SHOOT_INTERVAL;
		}
		public function turn(ang:Number):void {
			this.towerGraphics.turn(ang);
			move();
		}
		private function move():void {
			shoot = false;
			var prevAng:Number = towerGraphics.rotation;
			this.towerGraphics.move();
			if (_type == TowerType.WAVE) {// wave should not turn at all
				towerGraphics.turning = false;
				towerGraphics.rotation = prevAng;
			}else if (_type == TowerType.FIRE) {// fire should not turn while shooting
				if (_counterShoot < SHOOT_INTERVAL) {
					towerGraphics.turning = false;
					towerGraphics.rotation = prevAng;
				}
			}else if (_type == TowerType.SPARK) {// spark should not turn while shooting
				if (_counterShoot < SHOOT_INTERVAL) {
					towerGraphics.turning = false;
					towerGraphics.rotation = prevAng;
				}
			}
			// if tower is not turning(shooting), then forward the shoot timer 
			if (!towerGraphics.turning) {
				_counterShoot++;
				if (_counterShoot > SHOOT_INTERVAL) {
					_counterShoot = 0;
					shoot = true;
				}
			}
			
			this.towerGraphics.redraw();
		}
		private function typeConfig():void {
			switch (_type) {
				case TowerType.SLOW:
					SHOOT_INTERVAL = 20;
					break;
				case TowerType.WAVE:
					SHOOT_INTERVAL = 20;
					break;
				case TowerType.FIRE:
					SHOOT_INTERVAL = 20;
					break;
				case TowerType.SPARK:
					SHOOT_INTERVAL = 10;
					break;
				case TowerType.BULLET:
					SHOOT_INTERVAL = 2;
					break;
				case TowerType.ROCKET:
					SHOOT_INTERVAL = 20;
					break;
			}
			_counterShoot=SHOOT_INTERVAL;
		}
		public function get posX():int {
			return _posX;
		}
		public function get posY():int {
			return _posY;
		}
		public function get upgrades():uint {
			return _upgrades;
		}
		public function set upgrades(value:uint):void {
			_upgrades = value;
			this.towerGraphics.upgrade(_upgrades);
		}
		public function get type():TowerType {
			return _type;
		}
		public function get range():int {
			return _range;
		}
		public function get upgradeCost():int {
			return _upgradeCost;
		}
		public function get cost():int {
			return _cost;
		}
	}
}